<!DOCTYPE html>
<html>
<head>
    <title>Projectile Motion</title>
</head>
<body>
    <canvas id="physics_canvas" width="650" height="400" style="border:1px solid #d3d3d3;"></canvas>
    <script>
        const canvas = document.getElementById('physics_canvas');
        const ctx = canvas.getContext('2d');

        // Helper function to define an arrow path on the context
        function defineArrow(ctx, fromX, fromY, toX, toY) {
            const headlen = 12; // length of head in pixels
            const dx = toX - fromY;
            const dy = toY - fromY;
            const angle = Math.atan2(dy, dx);
            ctx.moveTo(fromX, fromY);
            ctx.lineTo(toX, toY);
            // Draw arrowhead
            ctx.moveTo(toX, toY);
            ctx.lineTo(toX - headlen * Math.cos(angle - Math.PI / 6), toY - headlen * Math.sin(angle - Math.PI / 6));
            ctx.moveTo(toX, toY);
            ctx.lineTo(toX - headlen * Math.cos(angle + Math.PI / 6), toY - headlen * Math.sin(angle + Math.PI / 6));
        }

        // --- Style and Configuration ---
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 2;
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        // --- Coordinates ---
        const originX = 400;
        const groundY = 300;
        const landingAX = 150;
        const labelOffsetY = 50;

        // --- Draw Ground Line ---
        ctx.beginPath();
        ctx.moveTo(50, groundY);
        ctx.lineTo(600, groundY);
        ctx.stroke();

        // --- Draw Points A and O ---
        ctx.font = "22px 'Arial'";
        // Point O (Origin)
        ctx.beginPath();
        ctx.arc(originX, groundY, 6, 0, 2 * Math.PI);
        ctx.fill();
        ctx.fillText('O', originX, groundY + 25);

        // Point A
        ctx.beginPath();
        ctx.arc(landingAX, groundY, 6, 0, 2 * Math.PI);
        ctx.fill();
        ctx.fillText('A', landingAX, groundY + 25);

        // --- Draw Trajectory of Ball A (dashed parabola) ---
        ctx.save();
        ctx.setLineDash([6, 6]);
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.moveTo(originX, groundY);
        const controlX = (originX + landingAX) / 2;
        const controlY = groundY - 160; // Controls the height of the parabola
        ctx.quadraticCurveTo(controlX, controlY, landingAX, groundY);
        ctx.stroke();
        ctx.restore();

        // --- Draw Velocity Vectors ---
        const v_len = 80;
        const v2_len = 160;

        ctx.beginPath(); // Start a new path for both arrows

        // Vector v (for Ball A)
        // Angle is 60 deg from horizontal-left, which is 120 deg from horizontal-right (positive x-axis).
        const angleA_rad = 120 * Math.PI / 180;
        const endXA = originX + v_len * Math.cos(angleA_rad);
        const endYA = groundY - v_len * Math.sin(angleA_rad); // Use minus for sin because canvas y-axis grows downwards
        defineArrow(ctx, originX, groundY, endXA, endYA);

        // Vector 2v (for Ball B)
        const angleB_rad = 30 * Math.PI / 180;
        const endXB = originX + v2_len * Math.cos(angleB_rad);
        const endYB = groundY - v2_len * Math.sin(angleB_rad);
        defineArrow(ctx, originX, groundY, endXB, endYB);

        ctx.stroke(); // Draw the arrows path

        // --- Draw Labels for vectors and angles ---
        ctx.font = "italic 22px 'Arial'";
        ctx.fillText('v', endXA - 20, endYA);
        ctx.fillText('2v', endXB + 20, endYB);

        ctx.font = "22px 'Arial'";
        
        // --- Draw Angle Arcs ---
        ctx.lineWidth = 1.5;

        // Angle 60°
        ctx.beginPath();
        // Arc from horizontal-left (PI rad) to vector angle (120 deg), counter-clockwise
        ctx.arc(originX, groundY, 45, Math.PI, angleA_rad, true);
        ctx.stroke();
        ctx.fillText('60°', originX - 65, groundY - 30);

        // Angle 30°
        ctx.beginPath();
        // Arc from horizontal-right (0 rad) to vector angle (-30 deg), clockwise
        ctx.arc(originX, groundY, 60, 0, -angleB_rad, true);
        ctx.stroke();
        ctx.fillText('30°', originX + 80, groundY - 25);

        // --- Draw T, D Label ---
        ctx.lineWidth = 2;
        const labelLineY = groundY + labelOffsetY;
        ctx.beginPath();
        ctx.moveTo(landingAX, labelLineY - 6);
        ctx.lineTo(landingAX, labelLineY + 6);
        ctx.moveTo(landingAX, labelLineY);
        ctx.lineTo(originX, labelLineY);
        ctx.moveTo(originX, labelLineY - 6);
        ctx.lineTo(originX, labelLineY + 6);
        ctx.stroke();
        
        ctx.font = "italic 22px 'Arial'";
        ctx.fillText('T, D', (landingAX + originX) / 2, labelLineY + 20);

    </script>
</body>
</html>